# -*- coding: utf-8 -*-
import logging
import time

from celery.schedules import crontab
from celery.task import task, periodic_task

from moments.models import WeChatUser

logger = logging.getLogger(__name__)


@task()
def very_very_slow_add(a, b):
    logger.info("好困啊，先睡会...")
    time.sleep(10)
    result = a + b
    logger.info("睡醒了，结果是：%s + %s = %s", a, b, result)
    return result


@task()
def send_mail(client, receiver, title, content):
    logger.info("[send mail] start")
    send_result = client.cmsi.send_mail({
        "receiver": receiver,
        "title": title,
        "content": content,
    })
    logger.info("[send mail] result: %s", send_result)


@periodic_task(run_every=crontab(minute="*"))
def remind_drink_water():
    logger.info("[remind_drink_water] start")
    for user in WeChatUser.objects.all():
        receiver = user.email
        title = f"Hey, {user.user.username}. It's time to drink water!"
        content = "Dun Dun Dun..."

        logger.info("send mail for drink water for address: %s", receiver)
        # 这里发送邮件
        logger.info("send mail success")
    logger.info("[remind_drink_water] end")
